package com.shxc.smarttrade.old.activity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.utils.sp.SpImp;
import com.shxc.smarttrade.old.utils.SpUtils;

import butterknife.BindView;
import butterknife.OnClick;
import ch.ielse.view.SwitchView;

/**
 * Created by jcdev1 on 2017/11/4.
 */

public class QuickOrderProtocolActivity extends BaseActivity {
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.swich_view)
    SwitchView swichView;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.tv_order_close)
    TextView tvOrderClose;
    @BindView(R.id.tv_text_explain)
    TextView tvTextExplain;

    SpImp spImp;

    private boolean isAgree = true;
    private boolean isOpen = false;

    private boolean quickOrderStatus = false;
    private boolean quickClosePosition = false;

    private int tag = -1;//0:快捷下单  1：快捷平仓

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quick_order_protocal);

        spImp = new SpImp(this);
        initView();
        initListener();
    }


    private void initView() {
        tag = getIntent().getIntExtra("tag", -1);
        quickOrderStatus = SpUtils.getBoolean(this, "quickOrderStatus", false);
        quickClosePosition = SpUtils.getBoolean(this, "quickClosePosition", false);
        if (tag == 0) {
            tvCenter.setText("快速下单协议");
            tvOrderClose.setText("快速下单");
            tvTextExplain.setText(getResources().getString(R.string.quick_order_text));
            if (quickOrderStatus)
                swichView.setOpened(true);
        } else {
            tvCenter.setText("快速平仓协议");
            tvOrderClose.setText("快速平仓");
            tvTextExplain.setText(getResources().getString(R.string.quick_close_positon_text));
            if (quickClosePosition)
                swichView.setOpened(true);
        }
    }

    private void initListener() {
        swichView.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
            @Override
            public void toggleToOn(SwitchView view) {
                if (isAgree) {
                    view.toggleSwitch(true);
                    isOpen = true;
                    if (tag == 0) {
                        SpUtils.putBoolean(QuickOrderProtocolActivity.this, "quickOrderStatus", true);
                        spImp.setFastOpen(true);
                    } else {
                        SpUtils.putBoolean(QuickOrderProtocolActivity.this, "quickClosePosition", true);
                        spImp.setFastClose(true);
                    }

                } else {
                    showToast("未同意免责声明");
                    view.toggleSwitch(false);
                }
            }

            @Override
            public void toggleToOff(SwitchView view) {
                view.toggleSwitch(false);
                isOpen = false;
                if (tag == 0){
                    SpUtils.putBoolean(QuickOrderProtocolActivity.this, "quickOrderStatus", false);
                    spImp.setFastOpen(false);
                }else{
                    SpUtils.putBoolean(QuickOrderProtocolActivity.this, "quickClosePosition", false);
                    spImp.setFastClose(false);
                }
            }
        });
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                QuickOrderProtocolActivity.this.isAgree = isChecked;
            }
        });
    }

    @OnClick(R.id.rl_left)
    public void onViewClicked() {
        finish();
    }
}
